# 序列化邮箱时对邮箱加密
from flask_restful import fields
from ext.encrypt import encrypt_email


class EncryptEmail(fields.Raw):

    def format(self, value):
        result = encrypt_email(value)
        return result


# 序列化用户信息
user_info_marshal_with = {
    'user_name': fields.String,
    'email': EncryptEmail,
    'portrait': fields.String,
}

# 序列化书籍简易信息
book_simple_info_marshal_with = {
    'id': fields.Integer,
    'title': fields.String,
    'author_name': fields.String(attribute='author.name'),
    'intro': fields.String,
    'cover': fields.String,
}

# 序列化书籍详细信息
book_detail_info_marshal_with = {
    'title':
    fields.String,
    'intro':
    fields.String,
    'cover':
    fields.String,
    'author_id':
    fields.Integer,
    'author_name':
    fields.String(attribute='author.name'),
    'description':
    fields.String,
    'score':
    fields.Float,
    'score_num':
    fields.Integer,
    'bookshelf_num':
    fields.Integer,
    'book_category_id':
    fields.Integer,
    'book_category_name':
    fields.String(attribute='book_category.name'),
    'book_tags':
    fields.List(fields.Nested({
        'id': fields.Integer,
        'name': fields.String,
    })),
}
